package service.impl;

import entity.AcademyEntity;
import entity.TeacherEntity;
import dao.TeacherDao;
import dao.impl.TeacherDaoImpl;
import service.AcademyService;
import service.TeacherService;
import util.UUIDUtil;
import vo.GetTeacherVO;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class TeacherServiceImpl implements TeacherService {
    private final TeacherDao teacherDao = new TeacherDaoImpl();
    private final AcademyService academyService = new AcademyServiceImpl();
    @Override
    public List<GetTeacherVO> getTeacherInfo(String name) {
        List<Map<String,Object>> mapList = teacherDao.getTeacherInfo(name);
        List<TeacherEntity> teacherEntityList = TeacherEntity.mapListToEntityList(mapList);
        return teacherEntityList.stream().map(teacher -> {
            GetTeacherVO vo = new GetTeacherVO();
            AcademyEntity academy = academyService.getAcademyInfoById(teacher.getAcademyId());
            vo.setTeacherId(teacher.getId());
            vo.setAcademyName(academy.getAcademyName());
            vo.setTeacherName(teacher.getTeacherName());
            vo.setAge(teacher.getAge());
            vo.setAcademyId(academy.getId());
            vo.setGender(teacher.getGender());
            vo.setPhoneNumber(teacher.getPhoneNumber());
            vo.setDegree(teacher.getDegree());
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public int addTeacherInfo(TeacherEntity teacher) {
        teacher.setId(UUIDUtil.getUUID());
        return teacherDao.addTeacherInfo(teacher);
    }

    @Override
    public int updateTeacherInfo(TeacherEntity teacher) {
        return teacherDao.updateTeacherInfo(teacher);
    }

    @Override
    public int deleteTeacher(String teacherId) {
        return teacherDao.deleteTeacher(teacherId);
    }

    @Override
    public int batchDeleteTeacher(List<String> ids) {
        return teacherDao.batchDeleteTeacher(ids);
    }

    @Override
    public List<TeacherEntity> getTeacherInfoByAcademyId(String academyId) {
        return TeacherEntity.mapListToEntityList(teacherDao.getTeacherInfoByAcademyId(academyId));
    }

    @Override
    public TeacherEntity getTeacherInfoById(String id) {
        return TeacherEntity.mapToEntity(teacherDao.getTeacherInfoById(id));
    }
}
